package com.hks360.car_treasure.mvp.equipment.model;

import android.content.Context;

import com.hks360.car_treasure.manager.CommonDataManager;
import com.hks360.car_treasure.model.BaseModel;
import com.hks360.car_treasure.model.CarLocation;
import com.hks360.car_treasure.model.CarState;
import com.hks360.car_treasure.mvp.BaseModelListener;
import com.hks360.car_treasure.util.GsonParseUtil;
import com.hks360.library.nohttp.HttpCallback;
import com.hks360.nohttp.Response;
import com.hks360.third_party_library.util.GsonService;

/**
 * Created by xiaote on 2016/3/30.
 */
public class EquipmentModelImpl implements EquipmentModel {
    private Context context;
    public EquipmentModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void bindUser(int what, String mobile, String vin, String tboxid, final BaseModelListener listener) {
        HttpCallback<String> httpCallback = new HttpCallback<String>() {
            @Override
            public void onSuccess(int what, Response<String> response) {
                BaseModel baseModel = GsonParseUtil.parseBase(response.get());
                listener.onSuccess(what,baseModel);
            }

            @Override
            public void onFailed(int what) {
                listener.onFailed(what);
            }

            @Override
            public void onStart(int what) {
                listener.onStart(what);
            }

            @Override
            public void onFinish(int what) {
                listener.onFinish(what);
            }
        };
        CommonDataManager.getInstance(context).bindUser(what,mobile,vin,tboxid,httpCallback);
    }

    @Override
    public void getCurrentState(int what, String tboxid, final EquipmentModelImpl.CarListener listener) {
        HttpCallback<String> httpCallback = new HttpCallback<String>() {
            @Override
            public void onSuccess(int what, Response<String> response) {
                CarState carState = GsonParseUtil.parseCarState(response.get());
                listener.onSuccess(what,carState);
            }

            @Override
            public void onFailed(int what) {
                listener.onFailed(what);
            }

            @Override
            public void onStart(int what) {
                listener.onStart(what);
            }

            @Override
            public void onFinish(int what) {
                listener.onFinish(what);
            }
        };
        CommonDataManager.getInstance(context).getCurrentState(what, tboxid, httpCallback);
    }

    @Override
    public void sendCommand(int what, String tboxid,String command, final BaseModelListener listener) {
        HttpCallback<String> httpCallback = new HttpCallback<String>() {
            @Override
            public void onSuccess(int what, Response<String> response) {
                BaseModel baseModel = GsonParseUtil.parseBase(response.get());
                listener.onSuccess(what,baseModel);
            }

            @Override
            public void onFailed(int what) {
                listener.onFailed(what);
            }

            @Override
            public void onStart(int what) {
                listener.onStart(what);
            }

            @Override
            public void onFinish(int what) {
                listener.onFinish(what);
            }
        };
        CommonDataManager.getInstance(context).sendCommand(what, tboxid, command, httpCallback);
    }

    public void readCurrentLocation(int what,String tboxid, final EquipmentModelImpl.CarLocationListener listener) {
        HttpCallback<String> httpCallback = new HttpCallback<String>() {
            @Override
            public void onSuccess(int what, Response<String> response) {
                CarLocation carLocation = GsonParseUtil.parseCarLocation(response.get());
                listener.onSuccess(what,carLocation);
            }

            @Override
            public void onFailed(int what) {
                listener.onFailed(what);
            }

            @Override
            public void onStart(int what) {
                listener.onStart(what);
            }

            @Override
            public void onFinish(int what) {
                listener.onFinish(what);
            }
        };
        CommonDataManager.getInstance(context).readCurrentLoaction(what,tboxid,httpCallback);
    }

    public interface CarListener {
        void onSuccess(int what, CarState carState);

        void onFailed(int what);

        void onStart(int what);

        void onFinish(int what);
    }

    public interface CarLocationListener {
        void onSuccess(int what, CarLocation carLocation);

        void onFailed(int what);

        void onStart(int what);

        void onFinish(int what);
    }
}
